/**
 * 员工接口
*/
var express = require('express');
var router = express.Router();

var sql = require('../db/index');

/**
 * @swagger
 *       /employee/saveOrUpdate:
 *       post:
 *           description: 员工管理
 *           tags: [员工]
 *           produces:
 *               - application/x-www-form-urlencoded
 *           parameters:
 *               - name: id,
 *                 description: 员工id
 *                 required: true
 *                 type: id
 *               - name: username,
 *                 description: 员工用户名
 *                 required: true
 *                 type: string
 *               - name: passsword,
 *                 description: 员工密码
 *                 required: false
 *                 type: string
 *               - name: telephone,
 *                 description: 员工手机号
 *                 required: true
 *                 type: string
 *               - name: gender,
 *                 description: 员工性别
 *                 required: false
 *                 type: string
 *            
 *           responses:
 *               200:
 *                   description: 操作成功
 *               500:
 *                   description: 服务器错误
 */

// 新增或者修改接口 req.body
router.post('/saveOrUpdate',async(req,res)=>{
    // 获取前端传递的参数
    const {id,username,password,telephone,gender} = req.body;
    // 如果id存在 修改操作 否则就是新增
    let mysql = '';
    if(id){
        mysql = `update base_user set username = ${'username'},password = ${password},telephone =${telephone},
        gender=${'gender'} where id=${id}
      `
    }else{
         mysql = `insert into base_user (id,username,password,status,telephone,gender)
     values(null,'${'username'}','${password}','正常','${telephone}','${'gender'}')`
    }
    await sql.query(mysql);
    // 给前端做出响应
    res.send({
        status:200,
        message:"操作成功",
        data:null,
        timestamp:new Date().getTime()
      })
    

})

/**
 * @swagger
 *      /employee/changeStatus:
 *      post:
 *          description: 应聘和解雇员工
 *          tags: [员工]
 *          parameters:
 *              - in: query
 *                name: id
 *                description: 员工id
 *                required: true
 *                schema:
 *                  type: number
 *              - in: query
 *                name: status
 *                description: 员工状态
 *                required: true
 *                schema:
 *                  type: string
 *          responses:
 *             '200':
 *                description: 返回员工列表
 */

// 上架和下架接口 req.body 需要前端传递参数 number status
// number 是为了知道那个产品被修改状态 status 知道点击的 是入职或者离职
router.post('/changeStatus',async(req,res)=>{
    // 获取前端传递的参数
    let {id,status} = req.body;
    if(status == '离职'){
        status = '离职'
    }else{
        status = '正常'
    }
    await sql.query(`update base_user set status = '${status}' where id = ${id}`);
    // 给前端做出响应
    res.send({
        status:200,
        message:status=='离职'?'离职成功':'入职成功',
        data:null,
        timestamp:new Date().getTime()
    })

})


/**
 * @swagger
 *    /employee/pageQuery:
 *    get:
 *        description: 分页多条件查询员工列表
 *        tags: [员工]
 *        parameters:
 *            - in: query
 *              name: page
 *              description: 页码
 *              required: true
 *              schema:
 *                type: number
 *            - in: query    
 *              name: pageSize
 *              description: 每页显示数量
 *              required: true
 *              schema:
 *                type: number
 *            - in: query    
 *              name: username
 *              description: 员工用户名
 *              required: false
 *              schema:
 *                type: string
 *            - in: query    
 *              name: status
 *              description: 员工状态
 *              required: false
 *              schema:
 *                type: string
 *            - in: query    
 *              name: telephone
 *              description: 员工电话号码
 *              required: false
 *              schema:
 *                type: number
 *        responses:
 *          '200':
 *            description: 返回产品列表  username
 */

// 分页模糊查询
router.get('/pageQuery', async function (req, res) {
    let { page, pageSize, name, status } = req.query;
    page = +page;
    pageSize = +pageSize;

    let result, result1;
    // 每页查询条数 id
    let offset = (page - 1) * pageSize;
    if (name && status ) {
      let mysql = `select * from base_user where name like '${'%' + name + '%'}' and status = '${status}' and  =  limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from base_user where name like '${'%' + name + '%'}' and status = '${status}' and  =  limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name && status) {
      let mysql = `select * from base_user where name like '${'%' + name + '%'}' and status = '${status}' limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from base_user where name like '${'%' + name + '%'}' and status = '${status}' limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name  ) {
      let mysql = `select * from base_user where name like '${'%' + name + '%'}' and  =  limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from base_user where name like '${'%' + name + '%'}' and  =  limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (status  ) {
      let mysql = `select * from base_user where status = '${status}' and  =  limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from base_user where status = '${status}'  and  =  limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    } else if (name || status ) {
      if (name) {
        let mysql = `select * from base_user where name like '${'%' + name + '%'}' limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from base_user where name like '${'%' + name + '%'}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      } else if (status) {
        let mysql = `select * from base_user where status ='${status}' limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from base_user where status = '${status}' limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      } else {
        let mysql = `select * from base_user where  =  limit ${pageSize} offset ${offset}`;
        result = await sql.query(mysql);
        let mysql1 = `select count(*) total from base_user where  =  limit ${pageSize} offset ${offset}`;
        result1 = await sql.query(mysql1);
      }
    } else {
      let mysql = `select * from base_user  limit ${pageSize} offset ${offset}`;
      result = await sql.query(mysql);
      let mysql1 = `select count(*) total from base_user  limit ${pageSize} offset ${offset}`;
      result1 = await sql.query(mysql1);
    }
    result = result.filter(item => {
      return item.length > 0;
    })
    console.log(result[0], '2222');
    total = result1[0][0].total;
    res.send({
      status: 200,
      message: '查询成功',
      data: result[0],
      total,
      timestamp: new Date().getTime()
    })
  })


module.exports = router;

